package com.taotao.search.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.ExceptionUtil;
import com.taotao.search.pojo.SearchResult;
import com.taotao.search.service.SearchService;

@Controller
public class SearchController {
	@Autowired
	private SearchService searchService;

	/**
	 * 发布搜索服务 (注意get请求传参中文乱码)
	 * 
	 * @param keyWords
	 * @param page
	 * @param rows
	 * @return
	 * @author: JSJ
	 */
	@RequestMapping("/q")
	@ResponseBody
	public TaotaoResult search(
			@RequestParam(defaultValue = "") String keywords,
			@RequestParam(defaultValue = "1") Integer page,
			@RequestParam(defaultValue = "20") Integer rows) {
		try {
			keywords = new String(keywords.getBytes("ISO-8859-1"), "UTF-8");
			SearchResult result = searchService.search(keywords, page, rows);
			return TaotaoResult.ok(result);
		} catch (Exception e) {
			e.printStackTrace();
			return TaotaoResult.build(500, ExceptionUtil.getStackTrace(e));
		}
	}
}
